<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of emi
 * 
 * @author Subha Sundar Das (subhasundar.das@gmail.com)
 */
class Emi extends MY_Controller {

    function emi() {
        parent :: __construct();
        $this->load->model('pickup_model');
        $this->load->model('location_model');
        $this->load->model('management_model');
        $this->load->model('listing_model');
        $this->load->model('requirement_model');
        $this->load->model('settings_model');
    }

    function index() {
        $this->template->title('EMI Calculator', $this->settings_model->get_setting('webtitle'))
                ->set_partial('header', 'header/header')
                ->set_partial('dealer', 'dealer/dealer')
                ->set_partial('recent', 'recent/post')
                ->set_partial('find_old', 'boxes/emi')
                ->set_partial('featured', 'boxes/featured')
                ->set_partial('customer_care', 'sidebar/customer_care')
                ->set_partial('social_share', 'sidebar/social_share')
                ->set_partial('search_reviews', 'sidebar/search_reviews')
                ->set_partial('recent_news', 'sidebar/recent_news')
                ->set_partial('recent_reviews', 'sidebar/recent_reviews')
                ->set_partial('footer', 'footer/footer')
                ->build('pages/index');
    }

    function emi_calculator() {
        $p = $_GET['p'];
        $r = $_GET['r'];
        $n = $_GET['n'];

        $loan = $p;
        $int = $r;
        $period = $n;
        $emi = 0;
        $tot_amt = 0;
        $tot_int = 0;

        $int = $int / 1200;
        $emi = ($loan * $int * pow(1 + $int, $period)) / (pow(1 + $int, $period) - 1);
        $emi = round($emi, 0);

        $tot_amt = round($emi * $period, 0);
        $tot_int = round($tot_amt - $loan, 0);


        print '
        <div class="rowElem" >
        <div class="fleft color_4" style="width: 150px" >
        <label><strong>EMI Amount :</strong></label>
        <label><strong>' . moneyFormat($emi) . '</strong></label>
        </div>

        <div class="fleft color_4" style="width: 150px" >
        <label><strong>Total :</strong></label>
        <label><strong>' . moneyFormat($tot_amt) . '</strong></label>
        </div>

        <div class="fleft color_4" style="width: 150px" >
        <label><strong>Total Interest:</strong></label>
        <label><strong>' . moneyFormat($tot_int) . '</strong></label>
        </div>

        </div>
        ';
    }

    function calculate() {
        $loan = 100000;
        $int = 8;
        $period = 24;
        $emi = 0;

        print "Loan Amount : $loan | Interest : $int | Period : $period" . "<p>";
        print "--------------------------------------------------------" . "</br>";

        // E = P * r * Math.Pow(1 + r, n) / ( Math.Pow(1 + r, n) - 1);     

        $int = $int / 1200;
        $emi = ($loan * $int * pow(1 + $int, $period)) / (pow(1 + $int, $period) - 1);
        $emi = round($emi, 0);
        print $emi . "</br>";
        print "--------------------------------------------------------" . "</br>";

        $tot = 0;




        $bal = $loan;
        for ($i = 1; $i <= $period; $i++) {
            $bal = $bal - $emi;
            $tot_emi = $tot + $emi;

            print $i . "->" . $emi . "->" . $tot_emi . "</br>";

            if ($bal >= 0) {
                $tot = $tot + $emi;
            }
        }


        print $tot / $period - 2;
    }

}

?>
